package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.event.PageButtonEvent;
import ru.spb.osll.myfit.client.manager.SuiteExecutingManager;
import ru.spb.osll.myfit.client.widget.*;
import ru.spb.osll.myfit.common.ExecutingResult;
import ru.spb.osll.myfit.common.SuiteExecutingResult;

public class SuiteResultView extends AbstractTestView {
  private ResizableResultTable m_resultTable = new ResizableResultTable();
  private ProgressBar m_progressBar = new ProgressBar();
  private ProgressLabel m_progressLabel = new ProgressLabel();
  private Label m_titleLabel = new Label();

  private SuiteExecutingManager m_suiteExecutingManager = new SuiteExecutingManager();
  private SuiteExecutingResult m_suiteExecutingResult;
  private String m_suitePath;
  private String m_suiteResultId;

  private PageButtonEventListener m_buttonEventListener = createButtonEventListener();

  public SuiteResultView() {
    setProgressVisible(true);
    m_titleLabel.setStyleName("testNameLabelStyle");
  }

  @Override
  protected Widget createContentPanel() {
    return m_resultTable.getWidget();
  }

  private PageButtonEventListener createButtonEventListener() {
    return new PageButtonEventListener() {

      @Override
      public void actionPerformed(PageButtonEvent event) {
        switch (event.getType()) {
          case BACK:
            Window.Location.assign(m_suitePath);
            break;
        }
      }
    };
  }

  private void updateProgress() {
    m_progressBar.updateView(m_suiteExecutingResult.getPercentage());
    m_progressLabel.updateView(m_suiteExecutingResult.getStatusString());
    if (!m_suiteExecutingResult.getState().equals(
            ExecutingResult.ExecutingState.FINISH)) {
      m_suiteExecutingManager.addListener(m_suiteResultId, m_progressBar);
      m_suiteExecutingManager.addListener(m_suiteResultId, m_progressLabel);
    }
  }

  public void displayResult(SuiteExecutingResult suiteExecutingResult) {
    m_suiteExecutingResult = suiteExecutingResult;
    m_suiteResultId = m_suiteExecutingResult.getId();
    m_suitePath = "suite?id=" + m_suiteExecutingResult.getName();

    m_suiteExecutingManager.addResultToListen(m_suiteExecutingResult);

    updateProgress();
  }

  public void updateResult(ExecutingResult result) {
    m_resultTable.updateResult(result);
  }

  @Override
  protected Widget createEditPanel() {
    VerticalPanel editPanel = (VerticalPanel)super.createEditPanel();
    editPanel.add(m_titleLabel);
    editPanel.add(createButtonPanel());
    return editPanel;
  }

  private Widget createButtonPanel() {
    Panel buttonPanel = new HorizontalPanel();
    buttonPanel.setWidth("100%");

    HorizontalPanel hPanel = new HorizontalPanel();
    hPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    hPanel.setSpacing(5);
    hPanel.add(new PageButton("Back to Test page", "backToTestButtonStyle",
            PageButtonEvent.PageButtonEventType.BACK, m_buttonEventListener));
    HorizontalPanel separator = new HorizontalPanel();
    separator.setWidth("20");
    buttonPanel.add(hPanel);
    buttonPanel.add(createProgressPanel());
    return buttonPanel;
  }

  private Widget createProgressPanel() {
    HorizontalPanel panel = new HorizontalPanel();
    panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    panel.setSpacing(5);
    panel.setWidth("100%");
    panel.add(m_progressLabel);
    panel.add(m_progressBar);
    panel.setCellWidth(m_progressBar, "250px");
    return panel;
  }

  private void setProgressVisible(boolean isVisible) {
    m_progressBar.setVisible(isVisible);
    m_progressLabel.setVisible(isVisible);
  }

  public void addResultToWidget(ExecutingResult result) {
    if (result != null) {
      m_resultTable.addResult(result);
    }
  }
}
